package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.RightsManagementService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
 *
 * 权限管理控制
 *
 * */

@RestController
@RequestMapping("/management")
public class RightsManagementController {
    @Reference
    private RightsManagementService rightsManagementService;

    @RequestMapping("/add")
    //新增
  public Result add(@RequestBody Permission permission){
        try {
            rightsManagementService.add(permission);
        }catch (Exception e){
            return new Result(false,MessageConstant.ADD_RIGHTSMANAGEMENT_FAIL);
        }
        return new Result(true, MessageConstant.ADD_RIGHTSMANAGEMENT_SUCCESS);
    }

   @RequestMapping("/findPage")
   //分页
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       PageResult pageResult = rightsManagementService.pageQuery(
               queryPageBean.getCurrentPage(),
               queryPageBean.getPageSize(),
               queryPageBean.getQueryString());
       return pageResult;
   }

    @RequestMapping("/delete")
    //删除
    public Result delete(Integer id){
        try {
            rightsManagementService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_RIGHTSMANAGEMENT_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_RIGHTSMANAGEMENT_SUCCESS);
    }

    @RequestMapping("/edit")
    //编辑
    public Result edit(@RequestBody Permission permission){
        try {
            rightsManagementService.edit(permission);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_RIGHTSMANAGEMENT_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_RIGHTSMANAGEMENT_SUCCESS);
    }

    @RequestMapping("/findById")
    //查询，根据ID
    public Result findById(Integer id) {
        try {
           Permission permission=rightsManagementService.findById(id);
           return new Result(true,MessageConstant.QUERY_RIGHTSMANAGEMENT_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
            //调用服务失败
            return new Result(false,MessageConstant.QUERY_RIGHTSMANAGEMENT_FAIL);
        }

    }
    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Permission> permissionList = rightsManagementService.findAll();
        if(permissionList != null && permissionList.size() > 0){
            Result result = new Result(true, MessageConstant.QUERY_RIGHTSMANAGEMENT_SUCCESS);
            result.setData(permissionList);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_RIGHTSMANAGEMENT_FAIL);
    }
}
